﻿using System;
using System.Web.UI;
using Solution.BusinessObject;

namespace Solution.Core
{
    public class PageBase : Page
    {
        /// <summary>
        /// Gets or sets this language of this page.
        /// </summary>
        protected string Language { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            GetLanguageSession();
            base.OnLoad(e);
        }

        /// <summary>
        /// Gets request language session.
        /// If fail, get default from database.
        /// If database does not set default to any language, get from web.config,
        /// else, returns [vi-VN]
        /// </summary>
        protected virtual void GetLanguageSession()
        {
            try
            {
                // if request
                if (Request["lang"] != null)
                {
                    Session["lang"] = Request["lang"];
                }
                else if (Session["lang"] == null)
                {
                    Session["lang"] = CommonBO.DefaultSiteLanguage.Code;
                }

                // return if getting is success
                if (Session["lang"] != null)
                {
                    this.Language = Session["lang"].ToString();
                    return;
                }

                // else, get from web config
                var defaultLanguage = System.Configuration
                    .ConfigurationManager.AppSettings["DefaultLanguage"];
                // if empty or cannot get
                Session["lang"] = string.IsNullOrEmpty(defaultLanguage)
                                      ? "vi-VN"
                                      : defaultLanguage;


                this.Language = Session["lang"].ToString();
            }
            catch (Exception ex)
            {
                CommonBO.WriteLog(ex);
            }
        }
    }
}